package com.geek.framework.study.config.microprofile.source;

import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public abstract class MapBaseConfigSource implements ConfigSource {

    final Map<String, String> configData;
    final String name;
    final int ordinal;

    public MapBaseConfigSource(String name, int ordinal) {
        this.configData = new HashMap<>();
        this.name = name;
        this.ordinal = ordinal;
    }


    protected Map<String, String> getConfigData() {
        if (configData.isEmpty()) {
            prepareConfigData(configData);
        }
        return configData;
    }

    protected abstract void prepareConfigData(Map<String, String> configData);


    @Override
    public Set<String> getPropertyNames() {
        return getConfigData().keySet();
    }

    @Override
    public String getValue(String key) {
        return getConfigData().get(key);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getOrdinal() {
        return ordinal;
    }

}
